
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 10/08/2011 12:34:16
-- Generated from EDMX file: C:\Users\weidas\documents\visual studio 2010\Projects\Lab2\Web\DataModel.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [Database.mdf];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_Group_User]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Group] DROP CONSTRAINT [FK_Group_User];
GO
IF OBJECT_ID(N'[dbo].[FK_Login_User]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Login] DROP CONSTRAINT [FK_Login_User];
GO
IF OBJECT_ID(N'[dbo].[FK_Message_receiver]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Message] DROP CONSTRAINT [FK_Message_receiver];
GO
IF OBJECT_ID(N'[dbo].[FK_Message_sender]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Message] DROP CONSTRAINT [FK_Message_sender];
GO
IF OBJECT_ID(N'[DataModelStoreContainer].[FK_User_Group_Group]', 'F') IS NOT NULL
    ALTER TABLE [DataModelStoreContainer].[User_Group] DROP CONSTRAINT [FK_User_Group_Group];
GO
IF OBJECT_ID(N'[DataModelStoreContainer].[FK_User_Group_User]', 'F') IS NOT NULL
    ALTER TABLE [DataModelStoreContainer].[User_Group] DROP CONSTRAINT [FK_User_Group_User];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[Group]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Group];
GO
IF OBJECT_ID(N'[dbo].[Login]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Login];
GO
IF OBJECT_ID(N'[dbo].[Message]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Message];
GO
IF OBJECT_ID(N'[dbo].[User]', 'U') IS NOT NULL
    DROP TABLE [dbo].[User];
GO
IF OBJECT_ID(N'[DataModelStoreContainer].[User_Group]', 'U') IS NOT NULL
    DROP TABLE [DataModelStoreContainer].[User_Group];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'Group'
CREATE TABLE [dbo].[Group] (
    [id] int IDENTITY(1,1) NOT NULL,
    [name] nvarchar(50)  NOT NULL,
    [owner] int  NOT NULL
);
GO

-- Creating table 'Login'
CREATE TABLE [dbo].[Login] (
    [id] int IDENTITY(1,1) NOT NULL,
    [logintime] datetime  NOT NULL,
    [user_id] int  NOT NULL
);
GO

-- Creating table 'Message'
CREATE TABLE [dbo].[Message] (
    [id] int IDENTITY(1,1) NOT NULL,
    [title] nvarchar(50)  NOT NULL,
    [text] nvarchar(max)  NOT NULL,
    [opened] bit  NOT NULL,
    [sender] int  NOT NULL,
    [receiver] int  NOT NULL,
    [archived] bit  NOT NULL,
    [timesent] datetime  NOT NULL,
    [deleted] bit  NOT NULL
);
GO

-- Creating table 'User'
CREATE TABLE [dbo].[User] (
    [id] int IDENTITY(1,1) NOT NULL,
    [email] nvarchar(50)  NOT NULL,
    [name] nvarchar(50)  NOT NULL,
    [password] nvarchar(256)  NOT NULL,
    [salt] nvarchar(256)  NOT NULL
);
GO

-- Creating table 'User_Group'
CREATE TABLE [dbo].[User_Group] (
    [Groupowner_id] int  NOT NULL,
    [Usermembers_id] int  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [id] in table 'Group'
ALTER TABLE [dbo].[Group]
ADD CONSTRAINT [PK_Group]
    PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- Creating primary key on [id] in table 'Login'
ALTER TABLE [dbo].[Login]
ADD CONSTRAINT [PK_Login]
    PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- Creating primary key on [id] in table 'Message'
ALTER TABLE [dbo].[Message]
ADD CONSTRAINT [PK_Message]
    PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- Creating primary key on [id] in table 'User'
ALTER TABLE [dbo].[User]
ADD CONSTRAINT [PK_User]
    PRIMARY KEY CLUSTERED ([id] ASC);
GO

-- Creating primary key on [Groupowner_id], [Usermembers_id] in table 'User_Group'
ALTER TABLE [dbo].[User_Group]
ADD CONSTRAINT [PK_User_Group]
    PRIMARY KEY NONCLUSTERED ([Groupowner_id], [Usermembers_id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [owner] in table 'Group'
ALTER TABLE [dbo].[Group]
ADD CONSTRAINT [FK_Group_User]
    FOREIGN KEY ([owner])
    REFERENCES [dbo].[User]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Group_User'
CREATE INDEX [IX_FK_Group_User]
ON [dbo].[Group]
    ([owner]);
GO

-- Creating foreign key on [user_id] in table 'Login'
ALTER TABLE [dbo].[Login]
ADD CONSTRAINT [FK_Login_User]
    FOREIGN KEY ([user_id])
    REFERENCES [dbo].[User]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Login_User'
CREATE INDEX [IX_FK_Login_User]
ON [dbo].[Login]
    ([user_id]);
GO

-- Creating foreign key on [receiver] in table 'Message'
ALTER TABLE [dbo].[Message]
ADD CONSTRAINT [FK_Message_receiver]
    FOREIGN KEY ([receiver])
    REFERENCES [dbo].[User]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Message_receiver'
CREATE INDEX [IX_FK_Message_receiver]
ON [dbo].[Message]
    ([receiver]);
GO

-- Creating foreign key on [sender] in table 'Message'
ALTER TABLE [dbo].[Message]
ADD CONSTRAINT [FK_Message_sender]
    FOREIGN KEY ([sender])
    REFERENCES [dbo].[User]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Message_sender'
CREATE INDEX [IX_FK_Message_sender]
ON [dbo].[Message]
    ([sender]);
GO

-- Creating foreign key on [Groupowner_id] in table 'User_Group'
ALTER TABLE [dbo].[User_Group]
ADD CONSTRAINT [FK_User_Group_Group]
    FOREIGN KEY ([Groupowner_id])
    REFERENCES [dbo].[Group]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Usermembers_id] in table 'User_Group'
ALTER TABLE [dbo].[User_Group]
ADD CONSTRAINT [FK_User_Group_User]
    FOREIGN KEY ([Usermembers_id])
    REFERENCES [dbo].[User]
        ([id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_User_Group_User'
CREATE INDEX [IX_FK_User_Group_User]
ON [dbo].[User_Group]
    ([Usermembers_id]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------